﻿using System;
using System.Web.Mail;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using MLY;
using MLY.Dal;
using MLY.Bll;
using System.IO;

namespace MLY.Reports.DataReports
{
    public partial class CanceledOrdersReport : System.Web.UI.Page
    {
        private class OrgData
        {
            public string OrganizationName { get; set; }
            public string CancelledPercent { get; set; }
            public string CanceledByQuantityPercent { get; set; }
        }        
        
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                DemoPanel.Visible = (Session["UserId"].ToString() == "39");
            }

            // checking logged in user
            if (clsUser.CurrentUserRoleId < 0)
            {
                Response.RedirectTo("/LTR/Login.aspx?ReturnTo=" + Request.Url.ToString());
            }

            // direction
            if (SessParam.Culture == "he")
            {
                //cssLiteral.Text = @"<link type=""text/css"" rel=""stylesheet"" href=""/Style/Rtl_Stylesheet.css"" />";
                DirectionPanel.Direction = ContentDirection.RightToLeft;
            }
            else
            {
                //cssLiteral.Text = @"<link type=""text/css"" rel=""stylesheet"" href=""/Style/Ltr_Stylesheet.css"" />";
                DirectionPanel.Direction = ContentDirection.LeftToRight;
            }
            
            GenerateReport();
        
        }

        public void GenerateReport()
        {
            int MyOrg = (int)Session["OrganizationId"];
            List<TableOrganization> Clients = MLY.Bll.clsOrganization.GetClientsByContractorId(MyOrg);
            List<TableOrganization> Contractors = MLY.Bll.clsOrganization.GetContractorsByClientId(MyOrg);
            List<OrgData> ContractorsList = new List<OrgData>();
            List<OrgData> ClientsList = new List<OrgData>();

            foreach (TableOrganization org in Clients)
            {
                OrgData o = new OrgData();
                o.OrganizationName = org.OrganizationName;
                o.CancelledPercent = (Math.Round((MLY.Bll.ReportsGenerator.CanceledWorkOrdersPercent(org.OrganizationId, MyOrg) * 100), 1)).ToString() + "%";
                o.CanceledByQuantityPercent =
                    (Math.Round((MLY.Bll.ReportsGenerator.CanceledWorkOrdersPercentByQuantity(org.OrganizationId, MyOrg) * 100), 1)).ToString() + "%";
                ClientsList.Add(o);
            }

            foreach (TableOrganization org in Contractors)
            {
                OrgData o = new OrgData();
                o.OrganizationName = org.OrganizationName;
                o.CancelledPercent = (Math.Round((MLY.Bll.ReportsGenerator.CanceledWorkOrdersPercent(MyOrg, org.OrganizationId) * 100), 1)).ToString() + "%";
                o.CanceledByQuantityPercent =
                    (Math.Round((MLY.Bll.ReportsGenerator.CanceledWorkOrdersPercentByQuantity(MyOrg, org.OrganizationId) * 100), 1)).ToString() + "%";
                ContractorsList.Add(o);
            }

            ContractorsGrid.DataSource = ContractorsList;
            ContractorsGrid.DataBind();
            CustomersGridView.DataSource = ClientsList;
            CustomersGridView.DataBind();

        }


    }
}
